/**
 * 
 */
package com.nikolvas.normdb;

import java.util.Map;
import com.nikolvas.man.Age;
import com.nikolvas.man.Sex;

/**
 * @author vasiliy
 *
 */
public class NormDb {

	
   public static synchronized NormDb getInstance(){
		if(instance == null){
			instance = new NormDb();
		}
		return instance;
	}
	
	public static synchronized void init(Map<Key, NormData> db){
		if(instance == null){
			instance = new NormDb();
		}
		instance.db = db;
	}
	
	public NormData getNormData(Age age, Sex sex){
		return db.get(new Key(age, sex));
	}
	    
    public class Key{
    	
    	private final Age age;
    	private final Sex sex;
    	
    	public Key(Age a, Sex s){
    		age = a;
    		sex = s;
    	}
    	
    	@Override
    	public int hashCode() {
    		final int prime = 31;
    		int result = 1;
    		result = prime * result + getOuterType().hashCode();
    		result = prime * result + ((age == null) ? 0 : age.hashCode());
    		result = prime * result + ((sex == null) ? 0 : sex.hashCode());
    		return result;
    	}
    	
    	@Override
    	public boolean equals(Object obj) {
    		if (this == obj)
    			return true;
    		if (obj == null)
    			return false;
    		if (getClass() != obj.getClass())
    			return false;
    		Key other = (Key) obj;
    		if (!getOuterType().equals(other.getOuterType()))
    			return false;
    		if (age == null) {
    			if (other.age != null)
    				return false;
    		} else if (!age.equals(other.age))
    			return false;
    		if (sex == null) {
    			if (other.sex != null)
    				return false;
    		} else if (!sex.equals(other.sex))
    			return false;
    		return true;
    	}
    	
    	private NormDb getOuterType() {
    		return NormDb.this;
    	}		
    }
	
    private static NormDb instance;
	
    private Map<Key, NormData> db;	
    private NormDb(){
    	//nothing
	}	
}
